from django.shortcuts import render_to_response
from edgdevelopment.fakemodule3.module3 import Logging#, SimulationRun
import edg.db.SimulationRun as SimulationRun
from edgdevelopment.reporter.models import RunID

letters = "ABCDEFGHJKLMNPQRTVWYZ" # IOSUX Confusing letters are left out.

def graphLosOverall( reses ):
  graph = {}
  graph['title']    = 'Length of Stay Overall'
  graph['id']       = 'losOverall'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getOverallLOS() } for i in range(len(reses)) ] }, )
  return graph

def graphLosBreakdown1( reses ):
  graph = {}
  graph['title']    = 'LOS: Triage in Waiting Room (W), Treatment (T)'
  graph['id']       = 'losBreakdown1'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': ':W', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getTriageLOS() } for i in range(len(reses)) ] },
    { 'name': ':T', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getTreatmentLOS() } for i in range(len(reses)) ] }, )
  return graph

# def graphLosBreakdown2( reses ):
#   graph = {}
#   graph['title']    = 'Length of Stay Breakdown 2: Door to Bed, Registration to Bed, Bed to Dispo'
#   graph['id']       = 'losBreakdown1'
#   graph['type']     = 'spreadGraph'
#   graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
#   graph['couples']  = (
#     { 'name': ':DtoB', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getLosDoorToBed() } for i in range(len(reses)) ] },
#     { 'name': ':RtoB', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getLosRegToBed() } for i in range(len(reses)) ] },
#     { 'name': ':BtoD', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getLosBedToDispo() } for i in range(len(reses)) ] }, )
#   return graph

def graphLosTriage( reses ):
  graph = {}
  graph['title']    = 'Length of Stay: Triage in Waiting Room'
  graph['id']       = 'losTriage'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getTriageLOS() } for i in range(len(reses)) ] }, )
  return graph

def graphLosTreatment( reses ):
  graph = {}
  graph['title']    = 'Length of Stay: Treatment'
  graph['id']       = 'losTreatment'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getTreatmentLOS() } for i in range(len(reses)) ] }, )
  return graph

# def graphLosRegToBed( reses ):
#   graph = {}
#   graph['title']    = 'Length of Stay: Registration to Bed'
#   graph['id']       = 'losRegToBed'
#   graph['type']     = 'spreadGraph'
#   graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
#   graph['couples']  = (
#     { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getLosRegToBed() } for i in range(len(reses)) ] }, )
#   return graph
# 
# def graphLosDoorToBed( reses ):
#   graph = {}
#   graph['title']    = 'Length of Stay: Door to Bed'
#   graph['id']       = 'losDoorToBed'
#   graph['type']     = 'spreadGraph'
#   graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
#   graph['couples']  = (
#     { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getLosDoorToBed() } for i in range(len(reses)) ] }, )
#   return graph
# 
# def graphLosBedToDispo( reses ):
#   graph = {}
#   graph['title']    = 'Length of Stay: Bed to Dispo'
#   graph['id']       = 'losBedToDispo'
#   graph['type']     = 'spreadGraph'
#   graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
#   graph['couples']  = ( 
#     { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getLosBedToDispo() } for i in range(len(reses)) ] }, )
#   return graph



def graphAcuityLOS( reses ):
  graph = {}
  graph['title']    = 'Length of Stay by Acuity'
  graph['id']       = 'losAcuity'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': ':1', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getAcuity1LOS() } for i in range(len(reses)) ] },
    { 'name': ':2', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getAcuity2LOS() } for i in range(len(reses)) ] },
    { 'name': ':3', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getAcuity3LOS() } for i in range(len(reses)) ] },
    { 'name': ':4', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getAcuity4LOS() } for i in range(len(reses)) ] },
    { 'name': ':5', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getAcuity5LOS() } for i in range(len(reses)) ] },
    { 'name': ':6', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getAcuity6LOS() } for i in range(len(reses)) ] }, )
  return graph

def graphAcuity1LOS( reses ):
  graph = {}
  graph['title']    = 'Length of Stay Acuity Level 1'
  graph['id']       = 'losAcuity1'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getAcuity1LOS() } for i in range(len(reses)) ] }, )
  return graph

def graphAcuity2LOS( reses ):
  graph = {}
  graph['title']    = 'Length of Stay Acuity Level 2'
  graph['id']       = 'losAcuity2'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getAcuity2LOS() } for i in range(len(reses)) ] }, )
  return graph

def graphAcuity3LOS( reses ):
  graph = {}
  graph['title']    = 'Length of Stay Acuity Level 3'
  graph['id']       = 'losAcuity3'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getAcuity3LOS() } for i in range(len(reses)) ] }, )
  return graph

def graphAcuity4LOS( reses ):
  graph = {}
  graph['title']    = 'Length of Stay Acuity Level 4'
  graph['id']       = 'losAcuity4'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getAcuity4LOS() } for i in range(len(reses)) ] }, )
  return graph

def graphAcuity5LOS( reses ):
  graph = {}
  graph['title']    = 'Length of Stay Acuity Level 5'
  graph['id']       = 'losAcuity5'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getAcuity5LOS() } for i in range(len(reses)) ] }, )
  return graph

def graphAcuity6LOS( reses ):
  graph = {}
  graph['title']    = 'Length of Stay Acuity Level 6'
  graph['id']       = 'losAcuity6'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getAcuity6LOS() } for i in range(len(reses)) ] }, )
  return graph

def graphUtil( reses ):
  graph = {}
  graph['title']    = 'Utilization of Resources'
  graph['id']       = 'util'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': ':Docs',   'spouses': [ { 'letter': letters[i], 'data': reses[i].getDoctorsUtil() }      for i in range(len(reses)) ] },
    { 'name': ':RNs',    'spouses': [ { 'letter': letters[i], 'data': reses[i].getNursesUtil() }       for i in range(len(reses)) ] },
    { 'name': ':TriRNs', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getTriageNursesUtil() } for i in range(len(reses)) ] },
    { 'name': ':Clerks', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getClerksUtil() }       for i in range(len(reses)) ] },
    { 'name': ':Beds',   'spouses': [ { 'letter': letters[i], 'data': reses[i].getBedsUtil() }         for i in range(len(reses)) ] }, )
  return graph

def graphUtilDocs( reses ):
  graph = {}
  graph['title']    = 'Utilization of Doctors'
  graph['id']       = 'utilDocs'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getDoctorsUtil() } for i in range(len(reses)) ] }, )
  return graph

def graphUtilRNs( reses ):
  graph = {}
  graph['title']    = 'Utilization of Nurses'
  graph['id']       = 'utilRNs'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getNursesUtil() } for i in range(len(reses)) ] }, )
  return graph

def graphUtilTriRNs( reses ):
  graph = {}
  graph['title']    = 'Utilization of Triage Nurses'
  graph['id']       = 'utilTriRNs'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getTriageNursesUtil() } for i in range(len(reses)) ] }, )
  return graph

def graphUtilClerks( reses ):
  graph = {}
  graph['title']    = 'Utilization of Clerks'
  graph['id']       = 'utilClerks'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getClerksUtil() } for i in range(len(reses)) ] }, )
  return graph

def graphUtilBeds( reses ):
  graph = {}
  graph['title']    = 'Utilization of Beds'
  graph['id']       = 'utilBeds'
  graph['type']     = 'spreadGraph'
  graph['colHeads'] = ( 'Min', '25%', 'Med', '75%', '95%', 'Max', )
  graph['couples']  = (
    { 'name': '', 'spouses': [ { 'letter': letters[i], 'data': reses[i].getBedsUtil() } for i in range(len(reses)) ] }, )
  return graph


graphFunctions = ( graphLosOverall,
                   
                   graphLosBreakdown1,
                   # graphLosBreakdown2,
                   graphLosTriage,
                   graphLosTreatment,
                   # graphLosRegToBed,
                   # graphLosDoorToBed,
                   # graphLosBedToDispo,
                    
                   graphAcuityLOS,
                   graphAcuity1LOS,
                   graphAcuity2LOS,
                   graphAcuity3LOS,
                   graphAcuity4LOS,
                   graphAcuity5LOS,
                   graphAcuity6LOS,
                   
                   graphUtil,
                   graphUtilDocs,
                   graphUtilRNs,
                   graphUtilTriRNs,
                   graphUtilClerks,
                   graphUtilBeds,
                 )

def reportOn( runIDs, request, user ):
        #print user.runid_set.all().values().get(id=int(runIDs))
	rs = [SimulationRun(int(user.runid_set.all().values().get(id=int(runID))['mod3RunID'])).getResults() for runID in runIDs.split(',')]
	graphs = [ graphFunction(rs) for graphFunction in graphFunctions ]
	cms = [ { 'name': letters[i],
            'config': (
              {'param': 'Docs',          'val': rs[i].getDoctorsCount(), },
              {'param': 'Nurses',        'val': rs[i].getNursesCount(),  },
              {'param': 'Beds',          'val': rs[i].getBedsCount(),    },
              {'param': 'Arrivals/Hr',   'val': rs[i].getRate(),         },
              ## {'param': 'Triage RNs',    'val': 2,   },
              ## {'param': 'Clerks',        'val': 3,   },
              ## {'param': 'Time (hrs)',    'val': 300, },
	    
            ),
          } for i in range(len(rs)) ]
	return render_to_response( 'reporter/report.html', { 
                'graphs': graphs,
                'cms': cms,
                'user':user,
		}, )








